package com.qumap.edititem.impl;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.*;
import com.qumap.edititem.EditItemInfo;
import com.qumap.item.ItemInfo;
import com.qumap.edititem.IItemEditArea;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: 17.05.11
 * Time: 19:38
 * To change this template use File | Settings | File Templates.
 */
public class IEA implements IItemEditArea  {

    @UiField
    SimplePanel item_edit_content;
    @UiField
    Button new_item_button;
    @UiField
    Button delete_button;
    @UiField
    ListBox item_list;
    @UiField
    Button save_button;
    @UiField
    Button reload_button;
    @UiField
    SimplePanel item_filter_content;
    @UiField
    CheckBox reload_on_select;
    @UiField
    Button clear_all_button;
    @UiField
    Button clear_typed;
    @UiField
    Button show_unsaved;


    interface IEAUiBinder extends UiBinder<HTMLPanel, IEA> {
	}

	private static IEAUiBinder ourUiBinder = GWT.create(IEAUiBinder.class);


	private Widget root;
	private List<EditItemInfo> available_items;


	public IEA() {
		root = ourUiBinder.createAndBindUi(this);
	}

	@Override
	public HasClickHandlers getNewButton() {
		return new_item_button;
	}

	@Override
	public HasClickHandlers getSaveButton() {
		return save_button;
	}

	@Override
	public HasClickHandlers getDeleteButton() {
		return delete_button;
	}

    @Override
    public HasClickHandlers getReloadButton() {
        return reload_button;
    }

    @Override
    public HasClickHandlers getClearAllButton() {
        return clear_all_button;
    }

    @Override
    public HasClickHandlers getClearTypedButton() {
        return clear_typed;
    }

    @Override
    public HasClickHandlers getShowUnsavedItems() {
        return show_unsaved;
    }

    @Override
    public HasChangeHandlers getItemList() {
        return item_list;
    }

    @Override
    public boolean needReloadOnSelect() {
        return reload_on_select.getValue();
    }

    @Override
	public void setValue(List<EditItemInfo> items) {
		available_items = items;
		for(ItemInfo item_to_add: available_items)
			item_list.addItem(item_to_add.getType());
        updateItemFilter(getSelectedItem());

	}

	@Override
	public ItemInfo getSelectedItem() {
		int selected_item = item_list.getSelectedIndex();
		return available_items.get(selected_item);
	}

    @Override
    public void updateItemInfo(ItemInfo active_item_info) {
        item_edit_content.clear();
        item_edit_content.add(active_item_info.getItemDisplay());
    }

    @Override
    public void updateItemFilter(ItemInfo active_item_info) {
        item_filter_content.clear();
        item_filter_content.add(active_item_info.getFilterDisplay());
    }

    @Override
	public Widget asWidget() {
		return root;
	}

}